package com.hourupdate.network.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.apache.log4j.Logger;

public class Tools 
{
	static final Logger logger = Logger.getLogger(Tools.class);
	public static String chkNull( String value ) {
		return (value == null)?(""):(value.trim());
	}
	/**
	 * @return yyyy-MM-dd-HH.mm.ss.SSSSSS
	 */
	public static String toDateTime(){
		Locale locale = new Locale("en", "US");
		Date date = new Date();
		SimpleDateFormat template = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSSSSS",locale);
		return template.format(date);
	}
	
	/**
	 * @return yyyyMMdd
	 */
	public static String toDate(){
		Locale locale = new Locale("en", "US");
		Date date = new Date();
		SimpleDateFormat template = new SimpleDateFormat("yyyyMMdd",locale);
		return template.format(date);
	}
	
    /**
     * @param dateString
     * @return yyyy-MM-dd-hh.mm.ss.SSSSSS
     */
    public static String parseDate(String dateString) {
    	Locale locale = new Locale("en", "US");
    	SimpleDateFormat template = null;
    	if(dateString.length() == 19){
//    		2013-05-30 16:10:14
    		template = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",locale);
    	}else if(dateString.length() == 25){
//    		Thu, 30 May 2013 17:33:01
    		template = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss",locale);
    	}else if(dateString.length() == 29){
//    		Mon, 03 Jun 2013 13:58:06 GMT
    		template = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z",locale);
    	}else{
//    		Thu, 30 May 2013 17:33:01 +0700
    		template = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z",locale);
    	}
		Date date = null;
		try {
			date = template.parse(dateString);
		} catch (ParseException e) {
			logger.error(e.getMessage());
		}
		
		template = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSSSSS",locale);
		return template.format(date);
	}
    
    /**
     * @param dateString
     * @return dd/MM/yyyy HH:mm:ss
     */
    public static String formatDate(String dateString) {
		return dateString.substring(8,10)+"/"+dateString.substring(5,7)+"/"+dateString.substring(0,4)+" "+dateString.substring(11,13)+":"+dateString.substring(14,16)+":"+dateString.substring(17,19);
    }
}
